package com.tz.pay.ali;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tz.pay.PayBean;
import com.tz.pay.PayParamUtil;
import com.tz.service.OrderService;

/**
 * 问题：解决跨域问题  springmvc jsonp的配置
 * TzAliPayController<BR>
 * 作者:Marlon  <BR>
 * 时间:2016年10月29日-下午3:02:52 <BR>
 * 邮箱:18093454447@163.com  <BR>
 * @version 1.0.0  <BR>
 * @link see:  <BR>
 */
@Controller
@RequestMapping("/ali")
public class TzAliPayController {

	@ResponseBody
	@RequestMapping("/pay")
	public String pay(PayBean payBean,HttpServletRequest request) {
//		String payBean2 = (String) request.getSession().getAttribute("payBean"); 
//		if(payBean2==null){//如果没点击支付，保存
//			request.getSession().setAttribute("payBean", payBean.getPno());
			//保存订单----订单状态未支付--status=0
			request.getSession().setAttribute("orderid", payBean.getPno());
			new OrderService().save(payBean);
			return PayParamUtil.goAliPay(payBean);
//		}else{
//			return PayParamUtil.goAliPay(payBean);
//		}
	}
	
	//在支付功能
	@ResponseBody
	@RequestMapping("/againPay/{id}")
	public String pay(@PathVariable("id")Integer orderId,HttpServletRequest request) {
		PayBean payBean = new OrderService().getPayBean(orderId);
		request.getSession().setAttribute("payBean", payBean);
		//保存订单----订单状态未支付--status=0
		return PayParamUtil.goAliPay(payBean);
	}
	
	@RequestMapping(value="/paySuccess")
	public String pay(HttpServletRequest request) {
		String orderid = (String) request.getSession().getAttribute("orderid");
		if(orderid==null)return "error";
		System.out.println(orderid);
		//修改订单状态.==status=1
//		int count = new OrderService().updateOrder(payBean.getPno(), payBean.getUserId());
		//清除会话中的数据
//		request.getSession().removeAttribute("payBean");
//		if(count>0){
//			return "success";
//		}else{
//			return "error";
//		}
		return "redirect:http://agent.lvbaolaws.com/ywcx/updateorder.do?orderid="+orderid;
	}
}